package com.material.factory.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.material.common.utils.DateUtils;
import com.material.common.utils.bean.BeanUtil;
import com.material.factory.domain.FactoryCategory;
import com.material.factory.mapper.FactoryCategoryMapper;
import com.material.factory.vo.FactoryInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.material.factory.mapper.FactoryInfoMapper;
import com.material.factory.domain.FactoryInfo;
import com.material.factory.service.IFactoryInfoService;

/**
 * 工厂信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class FactoryInfoServiceImpl implements IFactoryInfoService 
{
    @Autowired
    private FactoryInfoMapper factoryInfoMapper;
    @Autowired
    private FactoryCategoryMapper factoryCategoryMapper;

    /**
     * 查询工厂信息
     * 
     * @param id 工厂信息主键
     * @return 工厂信息
     */
    @Override
    public FactoryInfo selectFactoryInfoById(Long id)
    {
        return factoryInfoMapper.selectFactoryInfoById(id);
    }

    /**
     * 查询工厂信息列表
     * 
     * @param factoryInfo 工厂信息
     * @return 工厂信息
     */
    @Override
    public List<FactoryInfoVo> selectFactoryInfoList(FactoryInfo factoryInfo)
    {
        List<FactoryCategory> factoryCategories = factoryCategoryMapper.selectFactoryCategoryList(null);
        Map<Long, FactoryCategory> factoryCategoryMap = factoryCategories.stream().collect(Collectors.toMap(FactoryCategory::getId, f -> f, (existing, replacement) -> replacement));
        List<FactoryInfo> factoryInfos = factoryInfoMapper.selectFactoryInfoList(factoryInfo);
        List<FactoryInfoVo> factoryInfoVos = BeanUtil.copyProperties(factoryInfos, FactoryInfoVo.class);
        factoryInfoVos.forEach(f->{
            FactoryCategory factoryCategory = factoryCategoryMap.get(f.getCategoryId());
            f.setCategoryName(factoryCategory.getName());
        });
        return factoryInfoVos;
    }

    /**
     * 新增工厂信息
     * 
     * @param factoryInfo 工厂信息
     * @return 结果
     */
    @Override
    public int insertFactoryInfo(FactoryInfo factoryInfo)
    {
        factoryInfo.setCreateTime(DateUtils.getNowDate());
        return factoryInfoMapper.insertFactoryInfo(factoryInfo);
    }

    /**
     * 修改工厂信息
     * 
     * @param factoryInfo 工厂信息
     * @return 结果
     */
    @Override
    public int updateFactoryInfo(FactoryInfo factoryInfo)
    {
        return factoryInfoMapper.updateFactoryInfo(factoryInfo);
    }

    /**
     * 批量删除工厂信息
     * 
     * @param ids 需要删除的工厂信息主键
     * @return 结果
     */
    @Override
    public int deleteFactoryInfoByIds(Long[] ids)
    {
        return factoryInfoMapper.deleteFactoryInfoByIds(ids);
    }

    /**
     * 删除工厂信息信息
     * 
     * @param id 工厂信息主键
     * @return 结果
     */
    @Override
    public int deleteFactoryInfoById(Long id)
    {
        return factoryInfoMapper.deleteFactoryInfoById(id);
    }
}
